package com.bydq.patrolsystem.controller;

import com.bydq.patrolsystem.entity.PsFunction;
import com.bydq.patrolsystem.entity.PsUser;
import com.bydq.patrolsystem.service.PsFunctionService;
import com.bydq.patrolsystem.service.PsRolePremissionService;
import com.bydq.patrolsystem.service.PsRoleService;
import com.bydq.patrolsystem.vo.FunctionTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/premission")
@Controller
public class AuthorityController {

    @Autowired
    private PsFunctionService psFunctionService;

    @Autowired
    private PsRoleService psRoleService;

    @Autowired
    private PsRolePremissionService psRolePremissionService;

    @GetMapping("/authorityList")
    public String listAuthority(Model model){
        //查找所有未被禁用的角色
        model.addAttribute("roles",psRoleService.listStartRole());
        return "authorityList";
    }

    @GetMapping("/FunctionList")
    @ResponseBody
    public List<FunctionTreeVo> getAllFunction(
            @RequestParam(value = "roleId" , required = false )
                    Integer id){

        List<PsFunction> psFunctions = psFunctionService.listParent();
        //将function集合转化为前端需要的树型菜单集合
        return psFunctionService.functionPoToVo(psFunctions);
    }

    @GetMapping("/toAdd")
    public String toAdd(Integer id , Model model){
        if(id!=null){
            model.addAttribute("id",id);
        }
        return "functionAdd";
    }

    @GetMapping("/toEdit")
    public String toEdit(Integer id , Model model){
        PsFunction node = psFunctionService.getById(id);
        model.addAttribute("node",node);
        model.addAttribute("parent",psFunctionService.getById(node.getParentId()));
        return "functionEdit";
    }

    @PostMapping("/operationFunciton")
    @ResponseBody
    public boolean addFunction(PsFunction function , HttpSession session){
        PsUser user = (PsUser)session.getAttribute("user");
        //当前用户名加时间错
        function.setFunctionCode(user.getUserCode()+System.currentTimeMillis());
        function.setCreatedBy(user.getUserCode());
        return psFunctionService.saveOrUpdate(function);
    }

    @PostMapping("/delFunction/{id}")
    @ResponseBody
    public boolean delFunction(@PathVariable Integer id){
        return psFunctionService.delFunctionAndChildren(id);
    }

    @PostMapping("/getFunctionIdsByRoleId/{id}")
    @ResponseBody
    public Object[] getFunctionIdsByRoleId(@PathVariable Long id){
      return  psRolePremissionService.listFunctionIdsByRoleId(id);
    }

    @PostMapping("/setFunction")
    @ResponseBody
    public boolean setFunction(Long roleId,@RequestBody List<FunctionTreeVo> functionTreeVo){
        //给对应的角色分配权限
        return psRolePremissionService.setRoleAndPremission(roleId,functionTreeVo);
    }

}
